package lightswitch.server.code.object;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import lightswitch.server.code.util.TypeOfData;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
 * 
 * @author HieuPT
 *
 */
public class DataRow {
    private List<MyEntry> listOfEntries;
    
    public static DataRow convertDataRow(DataRow dr, String aliasName){
        DataRow result = new DataRow();
        
        for (MyEntry entry : dr.listOfEntries){
            result.addEntry(MyEntry.convertEntry(entry, aliasName));
        }
        
        return result;
    }
    
    public boolean addNullEntry(String nameEntry){
        return listOfEntries.add(new MyEntry(nameEntry.toLowerCase(), "", "char"));
    }
    
    public DataRow(){
        listOfEntries = new ArrayList<MyEntry>();
    }
    public DataRow(int numOfColumn)// crete datarow belongs to tb
    {
    	 listOfEntries = new ArrayList<MyEntry>();
    	 for(int i=0;i<numOfColumn;i++)
    	 {
	    	 MyEntry en = new MyEntry();
	    	
	    	 en.SetDataType(TypeOfData.CHAR);
	    	 en.setValue("null");
	    	 listOfEntries.add(en);
    	 }
    	 
    }
    public DataRow(DataRow row)
    {
    	listOfEntries = new ArrayList<MyEntry>();
    	for(int i=0;i<row.listOfEntries.size();i++)
    		addEntry(new MyEntry(row.getEntry(i)));
    }
    
    
    public MyEntry getEntry(int index){
        return listOfEntries.get(index);
    }
    
    public MyEntry getEntry(String name){
        for (MyEntry entry:listOfEntries){
            if (entry.getOriginName().equals(name)){
                return entry;
            }
        }
        return null;
    }
    
    public void setEntry(int index, MyEntry entry){
        listOfEntries.set(index, entry);
    }
    
    public void setEntry(int index, Object valueEntry){
        listOfEntries.get(index).setValue(valueEntry.toString());
    }
    
    public String getEntryDataType(String entryName)
    {
    	for(int i=0;i<listOfEntries.size();i++)
    	{
    		if(listOfEntries.get(i).getName().equals(entryName))
    		{
    		return	listOfEntries.get(i).getDataType();
    			
    		}
    	}
    	return "";
    }
    public boolean updateEntry(String entryName, String value )
    {
    	for(int i=0;i<listOfEntries.size();i++)
    	{
    		if(listOfEntries.get(i).getName().equals(entryName))
    		{
    			listOfEntries.get(i).setValue(value);
    			return true;
    		}
    	}
    	return false;
    }
    public String getValue(String entryName)
    {
    	for(int i=0;i<listOfEntries.size();i++)
    	{
    		if(listOfEntries.get(i).getName().equals(entryName))
    		{
    			return listOfEntries.get(i).getValue();
    			
    		}
    	}
    	return "";
    }
    public int getSize(){
        return listOfEntries.size();
    }
    
    public boolean addEntry(MyEntry entry){
        return listOfEntries.add(entry);
    }
    public MyEntry getValue(String tb, String col)
	{
		
		for(int i =0;i<listOfEntries.size();i++)
		{
			if(listOfEntries.get(i).getName().equals(tb+"."+col))
			{
				return listOfEntries.get(i);
			}
		}
		return null;
	}
    public static Element createElement(Document doc, DataRow dataRow){
        Element result = doc.createElement("DATA_ROW");
        for (int i = 0; i<dataRow.getSize(); i++){
            Element entry = MyEntry.createElement(doc, dataRow.getEntry(i));
            result.appendChild(entry);
        }
        return result;
    }
    
    public static DataRow createDataRowFromNode(Element dataRowElement){
        DataRow result = new DataRow();
        
        NodeList nodeList = dataRowElement.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                MyEntry entry = MyEntry.createEntryFromNode((Element) (node));
                result.addEntry(entry);                
            }
        }
        return result;
    }
    
    public Vector<Object> getChainOfValues(){
        Vector<Object> result = new Vector<Object>();
        for (int i = 0; i<listOfEntries.size(); i++){
            result.add(listOfEntries.get(i).getValue());
        }
        return result;
    }

    public void setViaName(String columnName, Object data) {
        for (int i = 0; i<listOfEntries.size(); i++){
            MyEntry entry =  listOfEntries.get(i);
            if (entry.getName().equals(columnName)){
                entry.setValue((String)data);
                listOfEntries.set(i, entry);
                return;
            }
        }
    }
    
    public boolean removeField(String fieldName){
        for (MyEntry entry : listOfEntries){
            if (entry.getName().equals(fieldName)){
                boolean entryDelete = listOfEntries.remove(entry);
                if (entryDelete) return true;
            }
        }
        return false;
    }
    public void removeField(int	index){
      
                listOfEntries.remove(index);
             
    }
    public String toString(){
        String result = "(";
        for (MyEntry entry : listOfEntries){
            result += entry.getValue() + ",";
        }
        result = result.substring(0, result.length()-1);
        result+=")";
        return result;
    }
    public void PlusDataRow(DataRow row)
    {
    	for(int i=0;i<row.getSize();i++)
    	{
    		listOfEntries.add(new MyEntry(row.getEntry(i)));
    	}
    }

    public boolean isLike(DataRow row)
    {
    	if(this.getSize()!=row.getSize())
    		return false;
    	for(int i=0;i<row.getSize();i++)
    	{
    		MyEntry en1 = this.getEntry(i);
    		MyEntry en2 = row.getEntry(i);
    		if(!en1.getValue().equals(en2.getValue()))
    			return false;
    		if(!en1.getDataType().equals(en2.getDataType()))
    			return false;
    		if(!en1.getName().equals(en2.getName()))
    			return false;
    	}
    	return true;
    }


    public boolean editEntry(Object value, int col) {
        String v = (String)value;
        getEntry(col).setValue(v);
        return true;
    }

    public boolean isHaveEntry(MyEntry entryIsRefed) {
        for (MyEntry entry : listOfEntries){
            if (entry.getOriginName().equals(entryIsRefed.getOriginName())){
                if (entry.getValue().equals(entryIsRefed.getValue())){
                    return true;
                }
            }
        }
        return false;
        
    }

    public void update(DataRow tempUpdate) {
        for (MyEntry entry : listOfEntries){
            for (MyEntry entryUpdate : tempUpdate.listOfEntries)
                if (entry.getOriginName().equals(entryUpdate.getOriginName()))
                    entry.setValue(entryUpdate.getValue());
        }
        
    }
    

}
